package display3d.alternativa {
	import alternativa.engine3d.containers.ConflictContainer;
	import alternativa.engine3d.materials.FillMaterial;
	import alternativa.engine3d.primitives.GeoSphere;

	import com.greensock.TweenMax;
	import com.greensock.easing.Quad;

	import flash.display.BlendMode;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Vector3D;
	import flash.utils.Timer;
	
	public class Spark3D extends ConflictContainer
	{
		private var centerParticle:GeoSphere;
		
		private var rotationTimer:Timer;
		private var particleTimer:Timer;
		
		private var particles:Vector.<GeoSphere>;
		
		public var scale:Number = 0;
		
		
		public function Spark3D()
		{
			super();
			
			this.filters = [new GlowFilter(0x3377FF, 1 , 20, 20, 2, 1, false)];
			
			this.blendMode = BlendMode.ADD;
			
			centerParticle = new GeoSphere(100, 2, false, new FillMaterial(0xFFFFFF, 1));
			addChild(centerParticle);
			centerParticle.scaleX = centerParticle.scaleY = centerParticle.scaleZ = .01;
					
			rotationTimer = new Timer(30, 0);
			rotationTimer.addEventListener(TimerEvent.TIMER, rotate);
			rotationTimer.addEventListener(TimerEvent.TIMER, resize);
			rotationTimer.start();
			
			particleTimer = new Timer(50, 0);
			particleTimer.start();
			particleTimer.addEventListener(TimerEvent.TIMER, addParticle);
			
			particles = new Vector.<GeoSphere>();
		}
		
		private function addParticle(evt:Event):void{
			
			if(scale == 0) return;
			
			var angleX:Number = Math.random()*(Math.PI*2);
			var angleY:Number = Math.random()*(Math.PI*2);
			var angleZ:Number = Math.random()*(Math.PI*2);
			
			var tempParticle:GeoSphere = particles.length > 0 ? particles.shift() : new GeoSphere(50, 1, false, new FillMaterial(0xFFFFFF,1));
			var distance:Number = centerParticle.boundMaxX*centerParticle.scaleX/2 + 150;
			tempParticle.x = Math.sin(angleZ)*distance + Math.cos(angleY)*distance;
			tempParticle.y = Math.cos(angleZ)*distance + Math.sin(angleX)*distance;
			tempParticle.z = Math.sin(angleY)*distance + Math.cos(angleX)*distance;
			addChild(tempParticle);
			tempParticle.scaleX = tempParticle.scaleY = tempParticle.scaleZ = .1;
			
			TweenMax.to(tempParticle, .5, {x:0, y:0, z:0, scaleX:centerParticle.scaleX, scaleY:centerParticle.scaleY, scaleZ:centerParticle.scaleZ, ease:Quad.easeOut, onComplete:removeParticle, onCompleteParams:[tempParticle]});
		}
		
		private function removeParticle(p_particle:GeoSphere):void{
			
			removeChild(p_particle);
			particles.push(p_particle);
		}
		
		private function resize(evt:Event):void{
			if(centerParticle.scaleX > scale) centerParticle.scaleX = centerParticle.scaleY = centerParticle.scaleZ -= .01;
			if(centerParticle.scaleX < scale) centerParticle.scaleX = centerParticle.scaleY = centerParticle.scaleZ += .01;
		}
		
		private function rotate(evt:Event):void{
			centerParticle.rotationY+=.1;
		}
		
		public function setPosition(pos:Vector3D):void{
			x = pos.x;
			y = pos.y;
			z = pos.z;
		}
	}
}